package com.icss.ah.fullReduce.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.fullReduce.service.FullReduceService;
import com.icss.ah.login.bean.UserInfoBean;

@Controller
public class FullReduceAction extends BaseAction{
	
	private static final long serialVersionUID = 1L;
	
	private final Logger errlog = Logger.getLogger("E");
	
	@Autowired
	private FullReduceService fullReduceService;
	
	private String rdeId; //满减活动id
	private String rdeName;
	private String begDate;
	private String endDate;
	private String useCtn;//使用条件
	private String cpsPrice; //扣减金额
	private String useDate;//生效时间
	private String status;
	private String reduceRange;
	private String productIds;
	private String flag;

	private String title;
	private String minPriceStr;
	private String maxPriceStr;
	private String firstSeries;
	private String secondSeries;
	private String timesReduce;
	
	public String toFullReduceListPage(){
		return "toFullReduceListPage";
	}
	
	public void queryFullReduceList(){
		try {
			Map<String, String> map = new HashMap<String, String>();
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("rdeName", rdeName);
			map.put("status", status);
			map.put("useDate", useDate);
			map.put("startIndex", getStartIndexMysql());
			map.put("endIndex", getEndIndexMysql());
			List<Map<String, Object>> list = fullReduceService.getFullReduceList(map);
			Long total = fullReduceService.getFullReduceListCount(map);
			dataGridJson = new EasyuiDataGridJson();
			dataGridJson.setRows(list);
			dataGridJson.setTotal(total);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceAction|异常：",e);
		} finally {
			str2Resp(JSONObject.fromObject(dataGridJson).toString());
		}
	}
	
	public String fullReducePage(){
		if(null!=rdeId&&!"".equals(rdeId)){
			Map<String, Object> info = fullReduceService.getFullReduceInfo(rdeId);
			int cp = ((BigDecimal) info.get("cpsPrice")).intValue();
			info.put("cpsPrice", cp);
			int uc = ((BigDecimal) info.get("useCtn")).intValue();
			info.put("useCtn", uc);
			request = getRequest();
			request.setAttribute("info", info);
			request.setAttribute("flag", request.getParameter("flag"));
		}
		return "fullReducePage";
	}
	
	public void save(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			String corpId = getBaseCorpId(); //从配置文件里取公众号id
			String userCode = getUserCodeFromSession();
			map.put("rdeId", UUID.randomUUID().toString());
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("rdeName", rdeName);
			map.put("begDate", begDate);
			map.put("endDate", endDate);
			map.put("cpsPrice", cpsPrice);
			map.put("useCtn", useCtn);
			map.put("status", status);
			fullReduceService.saveInfo(map);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("FullReduceAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public void updateInfo(){
		JSONObject json = new JSONObject();
		try {
			Map<String, String> map = new HashMap<String, String>();
			map.put("rdeId", rdeId);
			map.put("rdeName", rdeName);
			map.put("begDate", begDate);
			map.put("endDate", endDate);
			map.put("cpsPrice", cpsPrice);
			map.put("useCtn", useCtn);
			map.put("status", status);
			fullReduceService.updateInfo(map);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("FullReduceAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public void updateStatus(){
		JSONObject json = new JSONObject();
		try {
			fullReduceService.updateStatus(rdeId);
			json.put("flag", "1");
		} catch (Exception e) {
			json.put("flag", "0");
			e.printStackTrace();
			errlog.error("FullReduceAction|异常：",e);
		} finally {
			str2Resp(json.toString());
		}
	}
	
	public String toFullReduceInfoPage(){
		request = getRequest();
		Map<String, Object> bean = fullReduceService.getFullReduceInf(rdeId);
		String title = "满减活动详情";
		if("add".equals(flag)) title = "新增满减活动";
		else if("update".equals(flag)) title = "编辑满减活动";
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		map.put("level", "1");
		List<Map<String, Object>> yjfl = fullReduceService.getProductTypeList(map);
		request.setAttribute("yjfl", yjfl);
		request.setAttribute("flag", flag);
		request.setAttribute("rdeId", rdeId);
		request.setAttribute("bean", bean);
		request.setAttribute("title", title);
		return "toFullReduceInfoPage";
	}
	
	public void saveFullReduceInfo(){
		jsonText = new Json();
		try {
			Map<String, String> map = new HashMap<String, String>();
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			if("add".equals(flag))
				rdeId = UUID.randomUUID().toString();
			map.put("rdeId", rdeId);
			map.put("corpId", corpId);
			map.put("userCode", userCode);
			map.put("shopId", shopId);
			map.put("rdeName", rdeName);
			map.put("begDate", begDate);
			map.put("endDate", endDate);
			map.put("cpsPrice", cpsPrice);
			map.put("cpsPrice", cpsPrice);
			map.put("useCtn", useCtn);
			map.put("status", status);
			map.put("timesReduce", timesReduce);
			map.put("reduceRange", reduceRange);
			List<Map<String, String>> list = new ArrayList<Map<String,String>>();
			if(productIds != null && productIds.length() > 0){
				String[] ids = productIds.split(",");
				for(String id : ids){
					Map<String, String> m = new HashMap<String, String>();
					m.put("uuid", UUID.randomUUID().toString());
					m.put("rdeId", rdeId);
					m.put("proId", id);
					m.put("corpId", corpId);
					m.put("shopId", shopId);
					list.add(m);
				}
			}
			if("add".equals(flag))
				fullReduceService.saveFullReduceInfo(map,list);
			else
				fullReduceService.updateFullReduceInfo(map, list);
			jsonText.setSuccess(true);
			jsonText.setMsg(rdeId);
		} catch (Exception e) {
			jsonText.setSuccess(false);
			e.printStackTrace();
			errlog.error("FullReduceAction|saveFullReduceInfo|异常：",e);
		} finally {
			str2Resp(JSONObject.fromObject(jsonText).toString());
		}
	}
	
	public void getProductList(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			String serise ="";
			if(secondSeries==null||"".equals(secondSeries)||"0".equals(secondSeries)||"null".equals(secondSeries)){
				if(firstSeries!=null&&!"null".equals(firstSeries)&&!"0".equals(firstSeries)) 
				serise = firstSeries==null?"":firstSeries;
			}else{
				 serise = secondSeries;
			}
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			Map<String, String> map = new HashMap<String, String>();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			map.put("title", title);
			map.put("minPriceStr", minPriceStr);
			map.put("maxPriceStr", maxPriceStr);
			map.put("serise",serise);
			map.put("rdeId",rdeId);
			list = fullReduceService.getProductList(map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceAction|getProductList|异常：",e);
		} finally {
			str2Resp(JSONArray.fromObject(list).toString());
		}
	}
	
	private String level;
	public void getProductTypeList(){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();
			String corpId = userInfo.getCorpId();
			Map<String, String> map = new HashMap<String, String>();
			map.put("corpId",corpId);	//公众号id
			map.put("shopId", shopId);	//商铺id
			map.put("firstSeries", firstSeries);
			map.put("level", level);
			list = fullReduceService.getProductTypeList(map);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullReduceAction|getProductTypeList|异常：",e);
		} finally {
			str2Resp(JSONArray.fromObject(list).toString());
		}
		
	}
	
	public void delInfo(){
		String result = fullReduceService.delInfo(rdeId);
		str2Resp(result);
	}

	public String getRdeId() {
		return rdeId;
	}

	public void setRdeId(String rdeId) {
		this.rdeId = rdeId;
	}

	public String getRdeName() {
		return rdeName;
	}

	public void setRdeName(String rdeName) {
		this.rdeName = rdeName;
	}

	public String getBegDate() {
		return begDate;
	}

	public void setBegDate(String begDate) {
		this.begDate = begDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getUseCtn() {
		return useCtn;
	}

	public void setUseCtn(String useCtn) {
		this.useCtn = useCtn;
	}

	public String getCpsPrice() {
		return cpsPrice;
	}

	public void setCpsPrice(String cpsPrice) {
		this.cpsPrice = cpsPrice;
	}

	public String getUseDate() {
		return useDate;
	}

	public void setUseDate(String useDate) {
		this.useDate = useDate;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getReduceRange() {
		return reduceRange;
	}

	public void setReduceRange(String reduceRange) {
		this.reduceRange = reduceRange;
	}

	public String getProductIds() {
		return productIds;
	}

	public void setProductIds(String productIds) {
		this.productIds = productIds;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMinPriceStr() {
		return minPriceStr;
	}

	public void setMinPriceStr(String minPriceStr) {
		this.minPriceStr = minPriceStr;
	}

	public String getMaxPriceStr() {
		return maxPriceStr;
	}

	public void setMaxPriceStr(String maxPriceStr) {
		this.maxPriceStr = maxPriceStr;
	}

	public String getFirstSeries() {
		return firstSeries;
	}

	public void setFirstSeries(String firstSeries) {
		this.firstSeries = firstSeries;
	}

	public String getSecondSeries() {
		return secondSeries;
	}

	public void setSecondSeries(String secondSeries) {
		this.secondSeries = secondSeries;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getTimesReduce() {
		return timesReduce;
	}

	public void setTimesReduce(String timesReduce) {
		this.timesReduce = timesReduce;
	}

}
